package ThreadTest;
public class TicketTest2 {
    public static void main(String[] args) {
        TicketSell2 ticketSell2 = new TicketSell2();
        Thread thread1 = new Thread(ticketSell2);
        Thread thread2 = new Thread(ticketSell2);
        Thread thread3 = new Thread(ticketSell2);
        thread1.setName("窗口1");
        thread2.setName("窗口2");
        thread3.setName("窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class TicketSell2 implements Runnable{
    private static int tickets = 100;
    public static int getTickets() {
        return tickets;
    }
    @Override
    public void run() {
        while (true){
            synchronized (TicketSell2.class){ // 显示指定上锁的对象
                if(tickets > 0){
                    System.out.println(Thread.currentThread().getName()+":\t"+tickets + "\t->\t" +(tickets-1));
                    tickets--;
                }
            }
        }
    }
//    public static synchronized void Sell(){ // 静态，同步监视器（锁）不是this，是当前对象的类，即TicketSell
//        // public synchronized void Sell(){ // 非静态，同步监视器（锁）是this，即当前对象（类的一个实例对象），即thread1、thread2、thread3，方法错误
//        if(tickets > 0){
//            System.out.println(Thread.currentThread().getName()+":\t"+tickets + "\t->\t" +(tickets-1));
//            tickets--;
//        }
//    }
}